package com.example.javalib.java;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.List;
import java.util.Set;
import java.util.stream.Collectors;
import java.util.stream.IntStream;
import java.util.stream.Stream;

/**
 * @Description:
 * @author: zcs
 * @email: 1059737741@qq.com
 * create at 2022/2/15 14:09
 */
public class StreamTest {
    private static final List<Integer> integers = List.of(30, 40, 10, 20,50);

    //filter 过滤
    public static void main0(String[] args) {

        Set<Integer> collect = integers.stream().filter(i -> i > 20).collect(Collectors.toSet());
        //System.out.println(Arrays.toString(collect.toArray()));
        System.out.println(collect);//[50, 40, 30]
    }

    //map 对每个元素执行操作,不会改变原本数量
    public static void main1(String[] args) {

        List<String> collect = integers.stream().map(i -> i + "呵呵").collect(Collectors.toList());
        System.out.println(collect);//[30呵呵, 40呵呵, 10呵呵, 20呵呵, 50呵呵]
    }

    //mapToInt 转成IntSream
    public static void main2(String[] args) {
        List<Integer> list = List.of(2, 5, 1, 6);
        IntStream intStream = list.stream().mapToInt(i -> i);
        List<Integer> collect = intStream.boxed().collect(Collectors.toList()); //  boxed() 把int转成Integer??
        System.out.println(collect);//[2, 5, 1, 6]
  //    mapToDouble mapToLong 同理
    }

    //flatMap 类似把多个集合压平成一个集合
    public static void main3(String[] args) {
        List<List<Integer>> list = List.of(List.of(1, 2, 3), List.of(1, 2, 3));

        Stream<Integer> integerStream = list.stream().flatMap(Collection::stream);
        List<Integer> collect = integerStream.collect(Collectors.toList());
        System.out.println(collect);//[1, 2, 3, 1, 2, 3]
    }

    public static void main(String[] args) {
        List<Integer> integers = List.of(30, 40, 10, 20, 20, 10, 50, 20);
        List<Integer> collect = integers.stream().distinct().collect(Collectors.toList());
        System.out.println(collect);//[30, 40, 10, 20, 50]
    }


}
